//
// Created by YE on 2024/2/16.
//
#include "ych_stack.h"

void initSYNStack(SYNStack *stack) {
    stack->top = 0;
    stack->capacity = SYN_STACK_SIZE;
    stack->array = malloc(sizeof(T) * stack->capacity);
    if (stack->array == NULL) {
        throw(EXCEPTION_MEMORY);
    }
}

void pushSYNStack(SYNStack *stack, T item) {
    if (++stack->top >= stack->capacity) {
        stack->capacity <<= 1;
        stack->array = realloc(stack->array, sizeof(T *) * stack->capacity);
        if (stack->array == NULL) {
            throw(EXCEPTION_MEMORY);
        }
    }
    stack->array[stack->top] = item;
}

int isEmptySYNStack(SYNStack *stack) {
    return stack->top == 0;
}

T topSYNStack(SYNStack *stack) {
    return stack->array[stack->top];
}

T popSYNStack(SYNStack *stack) {
    if (stack->top <= 0) {
        throw(EXCEPTION_STACKOUTOFRANGE);
    }
    return stack->array[stack->top--];
}

void freeSYNStack(SYNStack *stack) {
    free(stack->array);
}

void initSEMStack(SEMStack *stack) {
    stack->top = 0;
    stack->capacity = SYN_STACK_SIZE;
    stack->array = malloc(sizeof(MInfo) * stack->capacity);
    if (stack->array == NULL) {
        throw(EXCEPTION_MEMORY);
    }
}

void pushSEMStack(SEMStack *stack, MInfo item) {
    if (++stack->top >= stack->capacity) {
        stack->capacity <<= 1;
        stack->array = realloc(stack->array, sizeof(MInfo) * stack->capacity);
        if (stack->array == NULL) {
            throw(EXCEPTION_MEMORY);
        }
    }
    stack->array[stack->top] = item;
}

int isEmptySEMStack(SEMStack *stack) {
    return stack->top == 0;
}

MInfo topSEMStack(SEMStack *stack) {
    return stack->array[stack->top];
}

MInfo popSEMStack(SEMStack *stack) {
    if (stack->top <= 0) {
        throw(EXCEPTION_STACKOUTOFRANGE);
    }
    return stack->array[stack->top--];
}

void freeSEMStack(SEMStack *stack) {
    free(stack->array);
}